package cn.lf.android.lawfirm.inputsource.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.inputsource.model.EntrustingTellEntity;

import static cn.lf.android.lawfirm.R.id.wtf;

/**
 * 拟委托方联系方式adapter
 */
public class AddEntrustingAdapter extends BaseAdapter {

    Context context;
    List<EntrustingTellEntity> list;
    private LayoutInflater inflater;

    public AddEntrustingAdapter(Context context, List<EntrustingTellEntity> list) {
        super();
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_add_entrusting, null);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.tell = (TextView) convertView.findViewById(R.id.tell);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
            viewHolder.wtf = (TextView) convertView.findViewById(wtf);
            viewHolder.divide = (View) convertView.findViewById(R.id.divide);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position == 0) {
            viewHolder.divide.setVisibility(View.GONE);
        } else {
            viewHolder.divide.setVisibility(View.VISIBLE);
        }
        viewHolder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                notifyDataSetChanged();
            }
        });
        if (null == list.get(position).wtf || list.get(position).wtf.length() == 0) {
            viewHolder.wtf.setVisibility(View.GONE);
            viewHolder.wtf.setText("");
        } else {
            viewHolder.wtf.setVisibility(View.VISIBLE);
            viewHolder.wtf.setText(list.get(position).wtf);
        }
        viewHolder.name.setText(list.get(position).entrustingName);
        viewHolder.tell.setText(list.get(position).entrustingTell);

        return convertView;
    }

    class ViewHolder {
        TextView name;
        ImageView img;
        TextView tell, wtf;
        View divide;
    }
}
